package com.itfreer.data.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import com.itfreer.utils.DateUtils;

/**
 * 类功能说明 定义值转换辅助类
 */
public class ValueConvertUtils {

	/**
	 * 转换数据类型
	 * 
	 * @throws Exception
	 */
	public static Object ConvertValue(String globalDataType, Object obj) throws Exception {
		if (null == globalDataType) {
			return obj;
		}
		if (obj == null) {
			return null;
		}

		Object value = null;
		String strObj = null;
		switch (globalDataType) {
		case "BOOLEAN":
			boolean b = false;
			strObj = obj.toString();
			if (strObj.equals("是") || strObj.equals("1")) {
				b = true;
			} else if (strObj.equals("否") || strObj.equals("0")) {
				b = false;
			} else {
				b = Boolean.parseBoolean(strObj);
			}
			value = b;
			break;
		case "STRING":
		case "CHAR":
		case "TEXT":
			strObj = obj.toString();
			value = strObj;
			break;
		case "DATE":
		case "DATETIME":
			if (obj instanceof String) {
				String dataString = obj.toString();
				if (dataString.trim().equals("")) {
					value = null;
				} else if (dataString.startsWith("{")) {
					String[] msgs = dataString.split(",");
					long time = -1;
					for (String item : msgs) {
						String[] keyValue = item.split("=");
						if (keyValue[0].toLowerCase().trim().equals("time")) {
							time = Long.parseLong(keyValue[1]);
							break;
						}
					}
					if (time != -1) {
						value = new Date(time);
					} else {
						value = obj;
					}
				} else {
					dataString = dataString.replace(".", "-");
					if (dataString.contains(":")) {
						value = DateUtils.convertStringToDateTime(dataString);
					} else if (dataString.contains("-")) {
						value = DateUtils.convertStringToDate(dataString);
					} else {
						// 有可能是纯数字
						Calendar calendar = new GregorianCalendar(1900, 0, -1);
						Date d = calendar.getTime();
						Calendar cal = Calendar.getInstance();
						cal.setTime(d);
						cal.add(Calendar.DATE, Integer.parseInt(dataString));
						value = cal.getTime();
					}
				}
			} else if (obj instanceof Map) {
				@SuppressWarnings("rawtypes")
				Map map = (Map) obj;
				Object time = map.get("time");
				value = new Date((long) time);
			} else if (obj instanceof Date) {
				value = obj;
			} else {
				throw new RuntimeException("不支持的数据类型！");
			}
			break;
		case "DECIMAL":
			Float f = 0.0f;
			strObj = obj.toString();
			if (!"".equals(strObj) && !"null".equals(strObj) && !"Null".equals(strObj)) {
				f = new Float(strObj);
			}
			value = f;
			break;
		case "DOUBLE":
			Double dd = 0.0d;
			strObj = obj.toString();
			if (!"".equals(strObj) && !"null".equals(strObj) && !"Null".equals(strObj)) {
				dd = new Double(strObj);
			}
			value = dd;
			break;
		case "SINGLE":
		case "INT16":
		case "INT":
		case "UINT16":
		case "UINT":
			Integer i = 0;
			strObj = obj.toString();
			if (!"".equals(strObj) && !"null".equals(strObj) && !"Null".equals(strObj)) {
				i = new Integer(strObj);
			}
			value = i;
			break;
		case "INT64":
		case "UINT64":
			Long l = 0l;
			strObj = obj.toString();
			if (!"".equals(strObj) && !"null".equals(strObj) && !"Null".equals(strObj)) {
				l = new Long(strObj);
			}
			value = l;
			break;
		case "BYTEARRAY":
			if (obj instanceof String) {
				value = ((String) obj).getBytes("UTF-8");
			} else {
				value = obj;
			}
			break;
		default:
			value = strObj;
		}

		return value;
	}
}
